package monster.com.cvh.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParseException;
import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.activity.NewsDetailsActivity;
import monster.com.cvh.bean.GetNoticeBean;
import monster.com.cvh.bean.NoticeBean;
import monster.com.cvh.util.TimeConvert;
import monster.com.cvh.util.TimeUtil;

/**
 * @author: monster
 * @data: 2018/3/29
 * @function: 通知界面的Adapter
 */
public class NoticeAdapter extends BaseMultiItemQuickAdapter<NoticeBean, BaseViewHolder> {
    List<GetNoticeBean.DataBean> dataBeans;
    public NoticeAdapter(List<NoticeBean> data, List<GetNoticeBean.DataBean> dataBeans) {
        super(data);
        addItemType(NoticeBean.RESET_PWD, R.layout.item_reset_pwd);
        addItemType(NoticeBean.ARTICLE,R.layout.item_article);
        this.dataBeans = dataBeans ;
    }

    @Override
    protected void convert(BaseViewHolder helper, NoticeBean item) {
        //获取当前时间戳
        long timeStamp = System.currentTimeMillis() / 1000;
        final GetNoticeBean.DataBean dataBean = dataBeans.get(helper.getLayoutPosition());
        Log.i(TAG, "convert: "+helper.getLayoutPosition());
        switch (item.getItemType()){
            case NoticeBean.RESET_PWD:

                try {
                    String s = TimeConvert.dateToStamp(dataBean.getTime());
                    long articleTime = timeStamp - Long.valueOf(s) / 1000;
                    String lastTime = TimeUtil.calculateTime(articleTime);
                    helper.setText(R.id.tv_item_reset_pwd_time,lastTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                    helper.setText(R.id.tv_ietm_reset_pwd,dataBean.getTitle());
                    helper.setText(R.id.tv_item_reset_pwd_centent,dataBean.getDesc());
                break;
            case NoticeBean.ARTICLE:
                try {
                    String s = TimeConvert.dateToStamp(dataBean.getTime());
                    long articleTime = timeStamp - Long.valueOf(s) / 1000;
                    String lastTime = TimeUtil.calculateTime(articleTime);
                    helper.setText(R.id.tv_item_artile_time,lastTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                    helper.setText(R.id.tv_item_article_title,dataBean.getTitle());
                    helper.setText(R.id.tv_item_artile_content,dataBean.getDesc());
                     Glide.with(mContext).load(dataBean.getArticle_image_url()).placeholder(R.mipmap.information_placeholderfigure).error(R.mipmap.information_placeholderfigure).into((ImageView) helper.getView(R.id.item_article_news_pic));
                     //跳转新闻
                     helper.getView(R.id.item_article_to_news).setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View view) {
                             Intent intent = new Intent(mContext, NewsDetailsActivity.class);
                             intent.putExtra("id",Integer.parseInt(dataBean.getAritcle_id()) );
                             intent.putExtra("isCollect", dataBean.isIs_collected());
                             intent.putExtra("reply",dataBean.getComment_count());
                             mContext.startActivity(intent);
                         }
                     });
                break;
        }
    }
}
